package hifiSpeakers;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.StartEvent;

/**
 * @author Aqeel
 * 
 */

public class HifiPlayer  implements ControllerListener{
	
	    Player player = null;
	    
	    public void init(String address) {
	      
	       try {
	          String mediaURL = address; 	//ip and port of data sink...
	          
	          MediaLocator ml = new MediaLocator(mediaURL);
	          
	          player = Manager.createPlayer(ml);
	          
	          player.addControllerListener(this);
	       } 
	       catch (Exception e) {
	          System.err.println("Could not create a player, GOT an exception "+e);
	       }
	       
	       player.start();
	    }
	    
	    // these functions can be used later to control player...
	    
	    public void start() {
	       player.start();
	    }
	    public void stop() {
	       player.stop();
	       player.deallocate();
	    }
	    public void destroy() {
	       player.close();
	    }

	    
	    //player will start and stop automatically on the arrival or ending of track.
	    
	   public synchronized void controllerUpdate(ControllerEvent event) {
	      
		   if (event instanceof RealizeCompleteEvent) {
	    	   
	   			if(event instanceof StartEvent)
	   			{
	   				player.start();
	   			
	   			}
		    	   if(event instanceof EndOfMediaEvent)
			   		{
			    		   player.stop();
			    		   player.deallocate();
			    		   player.close();
			    		   
			   		}
		   }
	   }
}